package com.fc.mall.portal.service.impl;

import com.fc.mall.mapper.UmsMemberFormMapper;
import com.fc.mall.model.UmsMember;
import com.fc.mall.model.UmsMemberForm;
import com.fc.mall.model.UmsMemberFormExample;
import com.fc.mall.portal.connector.WeChatConnector;
import com.fc.mall.portal.service.MemberFormService;
import com.fc.mall.portal.service.UmsMemberService;
import com.fc.mall.util.DateTimeHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author Sunny
 * Created by Sunny on 2019/7/18.
 */
@Service
public class MemberFormServiceImpl implements MemberFormService {

    @Autowired
    private UmsMemberService memberService;

    @Autowired
    private UmsMemberFormMapper memberFormMapper;

    @Autowired
    private WeChatConnector weChatConnector;

    @Override
    public int createMemberForm(String formId) {
        UmsMember member = memberService.getCurrentMember();
        UmsMemberForm memberForm = new UmsMemberForm();
        memberForm.setMemberId(member.getId());
        memberForm.setFormId(formId);
        memberForm.setCreateTime(new Date());
        return memberFormMapper.insertSelective(memberForm);
    }

    @Override
    public UmsMemberForm getFormIdByMember(Long memberId) {
        UmsMemberFormExample example = new UmsMemberFormExample();
        Date newDate = DateTimeHelper.addHours(new Date(), -176);
        example.createCriteria().andMemberIdEqualTo(memberId)
                .andCreateTimeGreaterThan(newDate);
        example.setOrderByClause("id desc");
        List<UmsMemberForm> formList = memberFormMapper.selectByExample(example);
        if(null == formList || formList.isEmpty())
        {
            return null;
        }
        return formList.get(0);
    }

    @Override
    public int delFormById(Long id) {
        return memberFormMapper.deleteByPrimaryKey(id);
    }
}
